<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set value="<%=request.getContextPath()%>" var="ctx"></c:set>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>客户管理</title>
    <meta name="keywords" content="关键字">
    <meta name="description" content="描述">
    <link rel="shortcut icon" href="favicon.ico"> <link href="${ctx}/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${ctx}/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <!-- Data Tables -->
    <link href="${ctx}/css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
    <link href="${ctx}/css/animate.css" rel="stylesheet">
    <link href="${ctx}/css/style.css?v=4.1.0" rel="stylesheet">
    <link href="${ctx}/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
    <style>
    .item-index{
    	width:80%;
    }
    .item-type{
    	width:140px;
    	height:30px;
    }
    </style>
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox">
                    <div class="ibox-title">
                        <div class="row">
                           <div class="col-sm-10">
                                <h3>客户列表</h3>
                           </div>
                        </div>
                    </div>
                    
                    <div class="ibox-content">
                        <!-- <div class="sk-spinner sk-spinner-wave">
                            <div class="sk-rect1"></div>
                            <div class="sk-rect2"></div>
                            <div class="sk-rect3"></div>
                            <div class="sk-rect4"></div>
                            <div class="sk-rect5"></div>
                        </div> -->
                         <div class="col-sm-1">
                                    <button type="button" style="margin-bottom: 20px;" class="btn btn-success btn-block" onclick = "add()">增加主菜单</button>
                          </div>
                        <table class="table table-striped table-bordered table-hover dataTables-example text-center">
                            <thead>
                                <tr>
                                    <th class="text-center" style="width:8%;">显示顺序</th>
                                    <th class="text-center" style="width:20%;">主菜单名称</th>
                                    <th class="text-center" style="width:20%;">触发动作</th>
                                    <th class="text-center" style="width:20%;">响应动作</th>
                                    <th class="text-center" style="width:20%;">操作</th>
                                </tr>
                            </thead>
                            <tbody id="tablebody">
                             
                            </tbody>
                        </table>
                        <div class="row">
                         <div class="col-sm-1">
                                    <button type="button" class="btn btn-success btn-block" onclick = "save()">保存</button>
                          </div>
                           <div class="col-sm-1">
                                    <button type="button" class="btn btn-success btn-block" onclick = "createMenu()">创建微信菜单</button>
                          </div>
                           <div class="col-sm-1">
                                    <button type="button" class="btn btn-success btn-block" onclick = "delMenu()">删除微信菜单</button>
                          </div>
                        </div>
                       
                    </div>
                </div>
            </div>
        </div>
    </div>
  <!-- 全局js -->
    <script src="${ctx}/js/jquery.min.js?v=2.1.4"></script>
    <script src="${ctx}/js/bootstrap.min.js?v=3.3.6"></script>

    <script src="${ctx}/js/jquery.jeditable.js"></script>

    <!-- Data Tables -->
    <script src="${ctx}/js/plugins/dataTables/jquery.dataTables.js"></script>
    <script src="${ctx}/js/plugins/dataTables/dataTables.bootstrap.js"></script>
    <script src="${ctx}/js/plugins/layer/layer.min.js"></script>
    <!-- 自定义js -->
    <script src="${ctx}/js/content.js?v=1.0.0"></script>
    <script src="${ctx}/js/plugins/sweetalert/sweetalert.min.js"></script>
    <script src="${ctx}/js/tree.js"></script>
    <!-- ECharts demo data -->

    <script>
    $(function(){
	    var tree;
	    getMenu();
    })
    function getMenu(){
    	$.ajax({
    		type:"POST",
			url: 'getMenu',
			success:function(data){
				tree = new MenuTree({
		        	data : data == ""?[]:JSON.parse(data).button,
		        	dom : $("#tablebody"),
		        	urlCheck : function(url){
		        		var reg = /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/;
		        		return reg.test(url);
		        	}
		        });
			},error: function(e){ 
				swal("获取菜信息失败", "网络连接错误", "error");
            }
    	})
    }
    /* var items = {
        "button":[
                  {	
                       "type":"view",
                       "name":"今日歌曲",
                       "url" : "http://baidu.com",
                       index : 1
                   },
                   {
                        "name":"菜单",
                        index : 0,
                        "sub_button":[
                        {	
                            "type":"view",
                            "name":"搜索",
                            "url":"http://www.soso.com/",
                            index : 0
                         },
                         {
                            "type":"view",
                            "name":"视频",
                            "url":"http://v.qq.com/",
                            index : 1
                         }]
                    }]
              } */
    	
        //保存至数据库      
    	function save(){
    		var res = tree.save(savaTreeCallBack,fail);
    		return res;
    	}
    	
    	//创建菜单
    	function createMenu(){
    		swal({
				title : "您确定要创建新微信菜单吗",
				text : "将会创建新的微信菜单",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "创建",
				closeOnConfirm : false
			}, function() {
	    		tree.save(savaTreeCallBack1,fail);			
			});
    	}
    	
    	
    	function createMenu1(){
   			$.ajax({
				type:"POST",
				url: 'createMenu',
				success:function(data){
					if(data.state == 'true'){
						swal("创建成功！", "微信菜单创建成功", "success");
					}else{
						swal("创建失败！", "微信菜单创建失败", "error");
					}
				},error: function(e){ 
					swal("创建失败！", "网络连接错误", "error");
	            }
			});
    	}
    	
    	//删除菜单
    	function delMenu(){
    		swal({
				title : "您确定要删除微信菜单吗",
				text : "将会删除微信公众号的微信菜单",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "删除",
				closeOnConfirm : false
			}, function() {
				$.ajax({
					type:"POST",
					url: 'delMenu',
					success:function(data){
						if(data.state == 'true'){
							swal("删除成功！", "微信菜单删除成功", "success");
						}else{
							swal("删除失败！", "微信菜单删除失败", "error");
						}
					},error: function(e){ 
						swal("删除失败！", "网络连接错误", "error");
		            }
				});
			});
    	}
    	
    	function add(){
    		tree.addItem();
    	}
    	
    	function savaTreeCallBack(json){
    		var str = JSON.stringify(json);
    		$.ajax({
				type:"POST",
				url: 'saveMenu',
				data: {'menu':str},
				success:function(data){
					if(data.state == 'true'){
						swal("保存成功！", "菜单保存成功", "success");
					}else{
						swal("保存失败！", "菜单保存失败", "error");
					}
				},error: function(e){ 
					swal("保存失败！", "网络连接错误", "error");
	            }
			});
    	}
    	
    	function savaTreeCallBack1(json){
    		var str = JSON.stringify(json);
    		$.ajax({
				type:"POST",
				url: 'saveMenu',
				data: {'menu':str},
				success:function(data){
					if(data.state == 'true'){
						createMenu1();
					}else{
						swal("保存失败！", "菜单保存失败", "error");
					}
				},error: function(e){ 
					swal("保存失败！", "网络连接错误", "error");
	            }
			});
    	}
    	
    	function fail(msg){
    		swal(msg);
    	}
          
    </script>
</body>
</html>
